package com.wsc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsc.domain.ChatHistory;
import com.wsc.mapper.ChatHistoryMapper;
import com.wsc.mapper.ConversationMapper;
import com.wsc.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ChatHistoryImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements IChatHistoryService {
    @Override
    public void saveChatHistory(String type, String chatId) {
        //先根据chatId查询会话记录
        Long count = lambdaQuery().eq(ChatHistory::getConversationId, chatId).count();
        if (count != null && count > 0) {
            return;
        }
        ChatHistory chatHistory = ChatHistory.builder()
                .userId(null)
                .conversationId(chatId)
                .title(type + chatId)
                .type(type)
                .createTime(LocalDateTime.now())
                .build();
        save(chatHistory);
    }

    @Override
    public List<String> getChatIds(String type, Long userId) {
        //查询所有
        type="mobile";
        List<ChatHistory> list = lambdaQuery()
                .eq(userId != null, ChatHistory::getUserId, userId)
                .eq(ChatHistory::getType, type).list();
        return list.stream().map(ChatHistory::getConversationId).toList();
    }
}